package com.lm56.web.search;

import com.lm56.common.MyResponseBody;
import com.lm56.common.Utils;
import com.lm56.web.sys.domain.User;
import com.lm56.web.sys.service.UserService;
import com.lm56.web.waybill.domain.WaybillTraceDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * Created by kelvin on 16/10/16.
 */
@Controller
@RequestMapping(value = "/")
public class SearchController {

    @Autowired
    SearchService searchService;

    @Autowired
    UserService userService;

    /**
     * 返加一个列表
     *
     * @param searchForm
     * @return
     */
    @RequestMapping(value = "search/1", method = RequestMethod.GET)
    public String searchByPhoneNo(SearchForm searchForm,Model model) {


        if(StringUtils.isBlank(searchForm.getPhoneNo())||!Utils.isMobilePhoneNo(searchForm.getPhoneNo())){
            searchForm.setPhoneNo("");
        }

        List<WaybillTraceDTO> waybillTraces = null;
        try {
            waybillTraces = searchService.getWaybillTraceByPhoneNo(searchForm);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("traceResult", waybillTraces);
        model.addAttribute("searchType","1");
        return "search/waybill_trace";
    }


    @RequestMapping(value = "search/2", method = RequestMethod.GET)
    public String searchByCompany(SearchForm searchForm,Model model) {

        List<WaybillTraceDTO> waybillTraces = null;
        try {
            waybillTraces = searchService.getWaybillTraceWaybillNoAndCompany(searchForm);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("traceResult", waybillTraces);
        model.addAttribute("searchType","1");
        return "search/waybill_trace";
    }


    @RequestMapping(value = "company/list")
    public String companyList(Model model) {
        List<User> companies =  userService.getAllCompanies();
        model.addAttribute("companyList",companies);
        return "search/company_list";
    }
}
